package com.imay.live.view.dialog;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.imay.live.R;

public class ConfirmDialogFragment extends CommonDialogFragment{
    protected static final String EXTRA_NAME_CANCELABLE = "cancelable";

    protected TextView mTitleTv;
    protected TextView mMessageTv;
    protected Button mNegativeBtn;
    protected Button mPositiveBtn;
    private OnClickListener mNegativeListener;
    private OnClickListener mPositiveListener;

    public static ConfirmDialogFragment newInstance(String title, String message, String positive, String negative, boolean cancelable) {
        ConfirmDialogFragment instance = new ConfirmDialogFragment();
        Bundle bundle = getBundle(title, message, positive, negative);
        bundle.putBoolean(EXTRA_NAME_CANCELABLE, cancelable);
        instance.setArguments(bundle);
        return instance;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_confirm;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTitleTv = $(R.id.confirm_tv_title);
        mMessageTv = $(R.id.confirm_tv_message);
        mNegativeBtn = $(R.id.confirm_btn_negative);
        mPositiveBtn = $(R.id.confirm_btn_positive);
        initData();
    }

    protected void initData() {
        //是否允许关闭
        boolean cancelable = getArguments().getBoolean(EXTRA_NAME_CANCELABLE, true);
        setCancelable(cancelable);
        if (mTitle != null) {
            mTitleTv.setVisibility(View.VISIBLE);
            mTitleTv.setText(mTitle);
        }
        mMessageTv.setText(mMessage);

        if (mNegativeBtn != null) {
            if (mNegative != null) {
                mNegativeBtn.setText(mNegative);
            }
            mNegativeBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (mNegativeListener != null) {
                        mNegativeListener.onClick(v);
                    }
                }
            });
        }
        if (mPositiveBtn != null) {
            if (mPositive != null) {
                mPositiveBtn.setText(mPositive);
            }
            mPositiveBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    if (mPositiveListener != null) {
                        mPositiveListener.onClick(v);
                    }
                }
            });
        }
    }

    public void setNegativeButton(OnClickListener listener) {
        mNegativeListener = listener;
    }

    public void setPositiveButton(OnClickListener listener) {
        mPositiveListener = listener;
    }
}
